/**********
 Tuple container class
 **********/
package qp.utils;
import java.util.StringTokenizer;
import java.util.Vector;
import java.io.Serializable;

/** Tuple - a simple object which holds a Vector
     of data */

public class Tuple implements Serializable {
    public Vector _data;



    public Tuple(Vector d){
	_data=d;
    }

    /** Accessor for data */
    public Vector data() {
        return _data;
    }

    public Object dataAt(int index){
	return _data.elementAt(index);
    }


    /** Checks whether the join condition is satisfied or not
     ** before performing actual join operation
     **/

    public boolean checkJoin(Tuple right, int leftindex, int rightindex){
	Object leftData = dataAt(leftindex);
	Object rightData = right.dataAt(rightindex);

	if(leftData.equals(rightData))
	    return true;
	else
	    return false;
    }



    /** Joining two tuples Without duplicate column elimination**/


    public Tuple joinWith(Tuple right){ //, Attribute leftAttr, Attribute rightAttr){
	Vector newData = new Vector(this.data());
	newData.addAll(right.data());
	return new Tuple(newData);
    }




	/** Compare two tuples in the same table on given attribute **/

    public static int compareTuples(Tuple left,Tuple right, int index){
	return compareTuples(left,right,index,index);
    }
   
    //********************************add by ZJX
    //return 0 represent equal, 1 represent not equal, -1 represent partly equal
    public static int AreTuplesEqual(Tuple left,Tuple right, int numOfColumn)
    {
    	boolean IsEqual=true;
    	boolean partEqual=false;
    	for(int i=0;i<numOfColumn;i++)
    	{
    		if(compareTuples(left,right,i)!=0)
    		{
    			IsEqual=false;
    		}
    		else
    			partEqual=true;
    	}
    	if(IsEqual)
    		return 0;
    	else if(partEqual)
    		return -1;
    	else
    		return 1;
    }

    	/** comparing tuples in different tables, used for join condition checking **/

    public static int compareTuples( Tuple left,Tuple right, int leftIndex, int rightIndex){
	Object leftdata = left.dataAt(leftIndex);
	Object rightdata = right.dataAt(rightIndex);
	if(leftdata instanceof Integer){
	    return ((Integer)leftdata).compareTo((Integer)rightdata);
	}else if(leftdata instanceof String){
		if(!leftdata.toString().contains("*"))
		{
	         return ((String)leftdata).compareTo((String)rightdata);
		}
		else
		{
			String leftString = (String) leftdata;
			StringTokenizer tokenizer = new StringTokenizer(leftString, "-:*");
			int leftYear = Integer.parseInt(tokenizer.nextToken());
			int leftMonth = Integer.parseInt(tokenizer.nextToken());
			int leftDay = Integer.parseInt(tokenizer.nextToken());
			int leftHour = Integer.parseInt(tokenizer.nextToken());
			int leftMinute = Integer.parseInt(tokenizer.nextToken());

			String rightString = (String) rightdata;
			StringTokenizer tokenizerC = new StringTokenizer(rightString, "-:*");
			int rightYear = Integer.parseInt(tokenizerC.nextToken());
			int rightMonth = Integer.parseInt(tokenizerC.nextToken());
			int rightDay = Integer.parseInt(tokenizerC.nextToken());
			int rightHour = Integer.parseInt(tokenizerC.nextToken());
			int rightMinute = Integer.parseInt(tokenizerC.nextToken());
			
			if (leftYear < rightYear
					|| ((leftYear == rightYear) && (leftMonth < rightMonth))
					|| ((leftYear == rightYear) && (leftMonth == rightMonth) && (leftDay < rightDay))
					|| ((leftYear == rightYear) && (leftMonth == rightMonth)
							&& (leftDay == rightDay) && (leftHour < rightHour))
					|| ((leftYear == rightYear) && (leftMonth == rightMonth)
							&& (leftDay == rightDay) && (leftHour == rightHour) && (leftMinute < rightMinute)))
				return -1;
			else if (leftYear > rightYear
					|| ((leftYear == rightYear) && (leftMonth > rightMonth))
					|| ((leftYear == rightYear) && (leftMonth == rightMonth) && (leftDay > rightDay))
					|| ((leftYear == rightYear) && (leftMonth == rightMonth)
							&& (leftDay == rightDay) && (leftHour > rightHour))
					|| ((leftYear == rightYear) && (leftMonth == rightMonth)
							&& (leftDay == rightDay) && (leftHour == rightHour) && (leftMinute > rightMinute)))
				return 1;
			
			else
				return 0;//value equal
			
		}

	}else if(leftdata instanceof Float){
	    return ((Float)leftdata).compareTo((Float)rightdata);
	}else{
	    System.out.println("Tuple: Unknown comparision of the tuples");
	    System.exit(1);
	    return 0;
	}
    }



}










